package com.twinkle.controller.classifier;

import com.alibaba.fastjson.JSONObject;
import com.twinkle.model.*;
import com.twinkle.service.ClassifierParamService;
import com.twinkle.service.ParamTrainResultService;
import com.twinkle.service.impl.ClassifierResultServiceImpl;
import com.twinkle.service.impl.DataBaseServiceImpl;
import com.twinkle.service.impl.MetadataColumnServiceImpl;
import com.twinkle.utils.ShellUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

/**
 * @title: DecisionTreeController
 * @description: 决策树算法类
 * @author: Paul
 * @date: 2023/3/13 9:39
 */
@Controller
@RequestMapping("/algo/decisiontree")
public class DecisionTreeController {

    @Autowired
    private DataBaseServiceImpl dataBaseService;
    @Autowired
    ClassifierResultServiceImpl classifierResultService;
    @Autowired
    private MetadataColumnServiceImpl metadataColumnService;
    @Autowired
    private ClassifierParamService classifierParamService;
    @Autowired
    private ParamTrainResultService paramTrainResultService;
    @Value("${data.algo.path}")
    private String algoPath;

    @RequestMapping("/addpage")
    public String addDecisionTreeModelPage(Model model){
        //获取数据源id
        List<DataBase> dataBases = dataBaseService.queryAll();
        model.addAttribute("dataBases", dataBases);
        List<String> tableList = metadataColumnService.getTableList(9);
        model.addAttribute("tableList", tableList);
        List<MetadataColumnEntity> metadataColumnEntities = metadataColumnService.getTableColumns(9, "data1");
        model.addAttribute("metadataColumnEntities", metadataColumnEntities);
//        return "algo_plat/decision_tree/decisiontree";
        model.addAttribute("algoName", "决策树");
        return "algo_plat/classifier/classifier";
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addDecisionTreeModel(Model model, @RequestBody JSONObject jsonObject) throws ParseException {
        //生成模型id
        System.out.println("jsonObject : " + JSONObject.toJSONString(jsonObject));
        String modelId = System.currentTimeMillis()+"";
        DecisionTreeParamUI classifierParam = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), DecisionTreeParamUI.class);
        classifierParam.setId(modelId);
        //保存模型参数
        classifierParamService.addClassifierParam(classifierParam);
        System.out.println(JSONObject.toJSONString(classifierParam));
        //调用决策树算法
        String shell = "python " + algoPath + "classifiers/decision_tree.py \"" + JSONObject.toJSONString(classifierParam).replaceAll("\"", "\\\\\"") + "\"";
        System.out.println(shell);
        ProcessResult processResult = ShellUtil.execShell(shell);
        System.out.println("processResult : " + JSONObject.toJSONString(processResult));
        JSONObject processResultJsonObject = JSONObject.parseObject(JSONObject.toJSONString(processResult));
        processResultJsonObject.put("modelId", modelId);
        return JSONObject.toJSONString(processResultJsonObject);
    }

    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public String getDecisionTreeResult(Model model, @Param("modelId") String modelId) throws ParseException {
        ClassifierResult classifierResult = classifierResultService.getClassifierResultByModelId(modelId);
        model.addAttribute("classifierResult", classifierResult);
        model.addAttribute("algoName", "决策树");
        return "algo_plat/classifier/classifier_result";
    }

    @ResponseBody
    @RequestMapping(value = "/paramtrain", method = RequestMethod.POST)
    public String addParamTrain(Model model, @RequestBody JSONObject jsonObject) throws ParseException {
        //生成模型id
        System.out.println("JSONObject : " + JSONObject.toJSONString(jsonObject));
//        DecisionTreeParamUI classifierParam = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), DecisionTreeParamUI.class);
        String modelId = System.currentTimeMillis()+"";
//        classifierParam.setId(modelId);
        jsonObject.put("id", modelId);
        //保存模型参数
        System.out.println(JSONObject.toJSONString(jsonObject));
        //调用聚类算法
        String shell = "python " + algoPath + "classifiers/decision_tree.py \"" + JSONObject.toJSONString(jsonObject).replaceAll("\"", "\\\\\"") + "\"";
        System.out.println(shell);
        ProcessResult processResult = ShellUtil.execShell(shell);
        JSONObject processResultJsonObject = JSONObject.parseObject(JSONObject.toJSONString(processResult));
        processResultJsonObject.put("modelId", modelId);
        System.out.println("processResult : " + JSONObject.toJSONString(processResultJsonObject));
        return JSONObject.toJSONString(processResultJsonObject);
    }

    @ResponseBody
    @RequestMapping(value = "/paramtrainresult", method = RequestMethod.GET)
    public String getParamTrainResult(Model model, @Param("modelId")String modelId) throws ParseException {
        ParamTrainResult paramTrainResult = paramTrainResultService.getParamTrainResultByModelId(modelId);
        return JSONObject.toJSONString(paramTrainResult);
    }

}
